package com.our123.cms.action.front;

import static com.our123.cms.Constants.TPLDIR_SPACE;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.our123.cms.entity.main.CmsSite;
import com.our123.cms.entity.main.CmsUser;
import com.our123.cms.manager.main.CmsUserMng;
import com.our123.cms.web.CmsUtils;
import com.our123.cms.web.FrontUtils;
import com.our123.common.web.RequestUtils;

@Controller
public class SpaceAct {
	private static final Logger log = LoggerFactory.getLogger(SpaceAct.class);

	public static final String SPACE_INDEX = "tpl.spaceIndex";
	public static final String SPACE_HOME = "tpl.spaceHome";

	@RequestMapping(value = "/space*.jsp", method = RequestMethod.GET)
	public String search(HttpServletRequest request, HttpServletResponse response, ModelMap model) {
		CmsSite site = CmsUtils.getSite(request);
		model.putAll(RequestUtils.getQueryParams(request));
		FrontUtils.frontData(request, model, site);
		FrontUtils.frontPageData(request, model);
		//用户名
		String u = RequestUtils.getQueryParam(request, "u");
		//栏目编号
		String c = RequestUtils.getQueryParam(request, "c");
		//排序方式
		String o = RequestUtils.getQueryParam(request, "o");
		CmsUser user = null;
		if (StringUtils.isNotBlank(u)) {
			try {
				Integer uid = Integer.parseInt(u);
				user = cmsUserMng.findById(uid);
			} catch (Exception e) {
				log.error("进入用户首页时查询用户失败", e);
			}
		}
		if (user != null && !user.getDisabled()) {
			model.addAttribute("author", user);
			model.addAttribute("c", c == null ? "" : c);
			model.addAttribute("o", o == null ? "2" : o);
			return FrontUtils.getTplPath(request, site.getSolutionPath(), TPLDIR_SPACE, SPACE_INDEX);
		} else {
			// 空间首页
			return FrontUtils.getTplPath(request, site.getSolutionPath(), TPLDIR_SPACE, SPACE_HOME);
		}
	}
	
	@Autowired
	private CmsUserMng cmsUserMng;
}
